#!/bin/bash
Red='\033[1;31m'
Green='\033[1;32m'
Yellow='\033[1;33m'
Blue='\033[1;34m'
Purple='\033[1;35m'
Cyan='\033[1;36m'
While='\033[1;37m'
Reset='\033[0m'
# 语言显示配置
#echo 'export LANG=en_US.UTF-8' | tee -a ~/.bashrc && ~/.bashrc
#source ~/.bashrc
echo -e "${Yellow}执行脚本前先切换到root用户，普通用户权限不足!!!${Reset}"
echo -e "${Red}确定要初始化请按回车，否则ctrl+c终止脚本。${Reset}"
read -p "" xxxx

# 检测ip地址或dns地址格式
function validateIpAndDns() {
    local ip_var_name=$1

    while true; do
        read -p "Input IP address($ip_var_name): " $ip_var_name

        #检测是否为空
        #
        if [ -z "${!ip_var_name}" ]; then
          echo -e "${Red}Input is empty.Please try again.${Reset}"
          continue
        fi

        #检测是否符合IP地址的格式
        if ! [[ ${!ip_var_name} =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
          echo -e "${Red}Invalid IP address fromat.Please try again.${Reset}"
          continue
        fi

        #输入符合要求，退出循环
        break
    done
}

# 修改ssh配置，实现通过客户端连接服务器
function OpenShhd() {
    # 修改ssh配置并配置root密码
    apt-get install openssh-server -y
    echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config
    echo 'root:zhangxiaofeng' | chpasswd
    systemctl restart sshd && systemctl status ssh -l --no-pager
}

#修改网卡名称为eth0
function ChangeNetworkInterface() {
   #修改网卡名称
   grep -i GRUB_CMDLINE_LINUX= /etc/default/grub | grep -i net &> /dev/null
   if [[ $? != "0" ]]; then
     sed -i 's@GRUB_CMDLINE_LINUX=""@GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0"@' /etc/default/grub
   fi
   update-grub
}

#旧网卡配置
function oldNetworkInterfaceConfig() {
cat > /etc/netplan/00-installer-config.yaml <<EOF
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: false
      dhcp6: false
      addresses:
        - ${ip_address}/24
      gateway4: ${ip_gateway}
      nameservers:
        addresses:
          - ${dns1_ip}
          - ${dns2_ip}
EOF
}

# 新网卡配置
function newNwrworkInterfaceConfig() {
cat > /etc/netplan/00-installer-config.yaml <<EOF
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: false
      dhcp6: false
      addresses:
        - ${ip_address}/24
      routes:
        - to: default
          via: ${ip_gateway}
      nameservers:
        addresses:
          - ${dns1_ip}
          - ${dns2_ip}
EOF
}

# 修改网卡配置
function SetupStaticIP() {
validateIpAndDns "ip_address"
echo -e "${Green}IP address: $ip_address${Reset}"

validateIpAndDns "ip_gateway"
echo -e "${Green}IP gateway: $ip_gateway${Reset}"

validateIpAndDns "dns1_ip"
echo -e "${Green}IP Dns1 ip: $dns1_ip${Reset}"

validateIpAndDns "dns2_ip"
echo -e "${Green}IP Dns1 ip: $dns2_ip${Reset}"

local ubuntuCodename=$(lsb_release -a 2>/dev/null|awk 'END{print $NF}')

case ${ubuntuCodename} in
xenial)
  oldNetworkInterfaceConfig
  ;;
bionic)
  oldNetworkInterfaceConfig
  ;;
*)
  newNwrworkInterfaceConfig
  ;;
esac
}

#修改dns配置
function SetDns() {
apt install resolvconf -y

cat > /etc/resolvconf/resolv.conf.d/head <<EOF
nameserver ${dns1_ip}
nameserver ${dns2_ip}
EOF

systemctl restart resolvconf
}

function main() {
    OpenShhd
    ChangeNetworkInterface
    SetupStaticIP
    SetDns
    echo -e "${Red}修改完成，请在10秒后重启服务器！！！${Reset}"
}

main